//
//  AlarmHeaderView.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/4/22.
//

import Foundation
import UIKit

class AlarmHeaderView: UIView {
    // 报警统计
    var cardsV: AlarmCardsView!

    // 筛选条件
    let filterV = AlarmFilterView()

    var deptId: Int64?
    var deptName: String?
    
    init(deptId: Int64?, deptName: String?) {
        super.init(frame: .zero)
        backgroundColor = .bg
        self.deptId = deptId
        self.deptName = deptName
        cardsV = AlarmCardsView(deptId: deptId, deptName: deptName)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - Setup
    
    func setupUI() {
        cardsV.updateUI(withModels: [
            AlarmCountByTypeModal(dictLabel: "劫警", dictValue: "0", total: 0),
            AlarmCountByTypeModal(dictLabel: "盗警", dictValue: "1", total: 0),
            AlarmCountByTypeModal(dictLabel: "火警", dictValue: "2", total: 0),
            AlarmCountByTypeModal(dictLabel: "电", dictValue: "3", total: 0),
            AlarmCountByTypeModal(dictLabel: "震动警", dictValue: "4", total: 0),
        ])
        addSubview(cardsV)
        cardsV.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalToSuperview().offset(10)
            make.height.equalTo(106)
        }
        
        addSubview(filterV)
        filterV.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(cardsV.snp.bottom).offset(10)
            make.height.equalTo(50)
            make.bottom.equalToSuperview()
        }
    }
}

class AlarmFilterView: UIView {
    // 筛选条件
    let dateCtl = BSStatusControl()
    let statusCtl = BSStatusControl()
    let compCtl = BSStatusControl()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        let baseV = CornerView()
        baseV.backgroundColor = .white
        baseV.layer.masksToBounds = true
        baseV.corners = SBRectCorner(topLeft: 10, topRight: 10, bottomLeft: 0, bottomRight: 0)
        addSubview(baseV)
        baseV.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        let itemW = (ScreenWidth - 20) / 2.0

        dateCtl.dataSource = [SelectPopModal(id: "1", name: "最近24小时报警"),
                              SelectPopModal(id: "2", name: "最近三天报警"),
                              SelectPopModal(id: "3", name: "最近七天报警")]
        dateCtl.key = "最近24小时报警"
        baseV.addSubview(dateCtl)
        dateCtl.snp.makeConstraints { make in
            make.left.top.bottom.equalToSuperview()
            make.centerY.equalToSuperview()
            make.width.equalTo(itemW)
        }
        
        let vLine1 = UIView()
        vLine1.backgroundColor = .hex("#E0E0E0")
        addSubview(vLine1)
        vLine1.snp.makeConstraints { make in
            make.left.equalTo(dateCtl.snp.right)
            make.centerY.equalToSuperview()
            make.height.equalTo(21)
            make.width.equalTo(0.5)
        }
        
        let dataSource = [
            SelectPopModal(name: "全部"),
            SelectPopModal(id: "0", name: "未处置"),
            SelectPopModal(id: "1", name: "已处置"),
        ]
        statusCtl.dataSource = dataSource
        statusCtl.key = "处置状态"
        baseV.addSubview(statusCtl)
        statusCtl.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.left.equalTo(dateCtl.snp.right)
            make.centerY.equalTo(dateCtl.snp.centerY)
            make.width.equalTo(itemW)
        }
        
//        let vLine2 = UIView()
//        vLine2.backgroundColor = .hex("#E0E0E0")
//        addSubview(vLine2)
//        vLine2.snp.makeConstraints { make in
//            make.left.equalTo(statusCtl.snp.right)
//            make.top.equalTo(vLine1.snp.top)
//            make.bottom.equalTo(vLine1.snp.bottom)
//            make.width.equalTo(vLine1.snp.width)
//        }
//
//        compCtl.dataSource = [SelectPopModal(name: "所属机构")]
//        compCtl.key = "所属机构"
//        addSubview(compCtl)
//        compCtl.snp.makeConstraints { make in
//            make.top.bottom.equalToSuperview()
//            make.left.equalTo(statusCtl.snp.right)
//            make.centerY.equalTo(dateCtl.snp.centerY)
//            make.width.equalTo(itemW)
//        }
    }
}


class AlarmHomeTopView: UIView {
    let stackView = UIStackView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateUI(withModal modals: [AlarmCbfModal?]) {
        stackView.removeAllSubViews()
        for modal in modals {
            let view = AlarmNotiRecordView()
            view.updateUI(withModal: modal)
            stackView.addArrangedSubview(view)
            view.snp.makeConstraints { make in
                make.height.equalTo(90)
                make.left.right.equalToSuperview()
            }
        }
    }
    
    func setupUI() {
        backgroundColor = .white
        layer.cornerRadius = 10
        layer.masksToBounds = true
            
        stackView.alignment = .fill
        stackView.axis = .vertical
        stackView.spacing = 0.0
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}

class AlarmNotiRecordView: UIView {
    let ic = UIImageView(image: UIImage(named: "ic_unarm_error"))
    let numL = UILabel()
    let titleL = UILabel()
    let dateL = UILabel()
    let statusL = UILabel()
    let line = UIView.createLine()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateUI(withModal modal: AlarmCbfModal?) {
        numL.text = modal?.tbDeviceName
        titleL.text = "\(modal?.tbDevicePosition ?? "")（\(modal?.tbWebsiteName ?? "")）"
        dateL.text = modal?.tbAlarmTime
        if modal?.tbAlarmType == "5" {
            ic.image = UIImage(named: "ic_unarm_error")
            statusL.text = "已撤防"
            statusL.textColor = .hex("#EA0001")
        }
        if modal?.tbAlarmType == "6" {
            ic.image = UIImage(named: "ic_unarm_success")
            statusL.text = "已布防"
            statusL.textColor = .hex("#008B27")
        }
    }

    func setupUI() {
        addSubview(ic)
        ic.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(12)
            make.top.equalToSuperview().offset(15)
            make.width.height.equalTo(17)
        }
        
        numL.textColor = .hex("#414A57")
        numL.font = .systemFont(ofSize: 14)
        addSubview(numL)
        numL.snp.makeConstraints { make in
            make.left.equalTo(ic.snp.right).offset(5)
            make.centerY.equalTo(ic.snp.centerY)
        }
        
        titleL.textColor = .hex("#414A57")
        titleL.font = .systemFont(ofSize: 14)
        addSubview(titleL)
        titleL.snp.makeConstraints { make in
            make.left.equalTo(numL.snp.left)
            make.top.equalTo(numL.snp.bottom).offset(8)
        }
        
        dateL.textColor = .hex("#414A57")
        dateL.font = .systemFont(ofSize: 14)
        addSubview(dateL)
        dateL.snp.makeConstraints { make in
            make.left.equalTo(titleL.snp.left)
            make.top.equalTo(titleL.snp.bottom).offset(6)
        }
        
        statusL.textColor = .hex("#0F499E")
        statusL.font = .systemFont(ofSize: 14)
        addSubview(statusL)
        statusL.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-12)
            make.centerY.equalTo(ic.snp.centerY)
        }
        
        line.backgroundColor = .hex("#EBEBEB")
        addSubview(line)
        line.snp.makeConstraints { make in
            make.left.equalTo(ic.snp.left)
            make.right.equalTo(statusL.snp.right)
            make.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
}
